package com.shs.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shs.bus.entity.Goods;
import com.shs.bus.entity.Inport;
import com.shs.bus.entity.Outport;
import com.shs.bus.entity.Provider;
import com.shs.bus.service.GoodsService;
import com.shs.bus.service.InportService;
import com.shs.bus.service.OutportService;
import com.shs.bus.service.ProviderService;
import com.shs.bus.vo.InportVo;
import com.shs.bus.vo.OutportVo;
import com.shs.sys.common.DataGridView;
import com.shs.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jxm
 * @since 2020-04-17
 */
@RestController
@RequestMapping("outport")
public class OutportController {

    @Autowired
    private OutportService outportService;

    @Autowired
    private ProviderService providerService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品退货
     * @return
     */
    @RequestMapping("loadAllOutport")
    public DataGridView loadAllOutport(OutportVo vo){
        Page<Outport> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Outport> wrapper = new QueryWrapper();
        wrapper.eq(vo.getProviderid()!=null && vo.getProviderid()!=0,"providerid",vo.getProviderid());
        wrapper.eq(vo.getGoodsid()!=null && vo.getGoodsid()!=0,"goodsid",vo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(vo.getOperateperson()),"operateperson",vo.getOperateperson());
        wrapper.ge(vo.getStartTime()!=null,"outputtime",vo.getStartTime());
        wrapper.le(vo.getEndTime()!=null,"outputtime",vo.getEndTime());
        wrapper.orderByDesc("outputtime");
        outportService.page(page,wrapper);
        List<Outport> records = page.getRecords();
        for (Outport record : records) {
            Provider provider = providerService.getById(record.getProviderid());
            if(null!=provider){
                record.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(record.getGoodsid());
            if(null!=goods){
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加退货单
     * @param id
     * @param number
     * @param remark
     * @return
     */
    @RequestMapping("addOutport")
    public ResultObj addOutport(Integer id, Integer number, String remark){
        try {
            outportService.addOutport(id,number,remark);
            return ResultObj.OPERATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }


}
